﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class departmentedit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["departmentid"]))
            {
                throw new Base.CustomException("缺少参数departmentid");
            }

            Hashtable department = PhoneBook.Group.Department.get(Request["departmentid"]);
            if (department == null)
            {
                throw new Base.CustomException("参数departmentid不正确");
            }

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (!user["groupid"].Equals(department["groupid"]))
            {
                throw new Base.CustomException("无权限修改此部门");
            }

            if (user["IsBOOper"] != DBNull.Value && !user["bo"].Equals(department["boid"]))
            {
                throw new Base.CustomException("无权限修改此部门");
            }

            if (!IsPostBack)
            {
                //分公司
                DataSet ds = PhoneBook.Group.BranchOffice.list(Commons.StrUtil.FormatValue(user["groupid"]));
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    ddlBo.DataSource = ds;
                    ddlBo.DataTextField = "boname";
                    ddlBo.DataValueField = "id";
                    ddlBo.DataBind();
                }

                ddlBo.Items.Insert(0, new ListItem("非分公司", ""));

                if (user["IsBOOper"] != DBNull.Value && Convert.ToBoolean(user["IsBOOper"]))
                {
                    ddlBo.SelectedValue = Commons.StrUtil.FormatValue(user["bo"]);
                    ddlBo.Enabled = false;
                }
                txb_departmentname.Text = Commons.StrUtil.FormatValue(department["departmentname"]);
                
                ddlBo.SelectedValue = Commons.StrUtil.FormatValue(department["boid"]);
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Hashtable department = PhoneBook.Group.Department.get(Request["departmentid"]);

            string content = string.Empty;
            if (department["departmentname"].ToString() != txb_departmentname.Text)
            {
                content = string.Format("修改了部门信息。名称：{0}->{1}", department["departmentname"], txb_departmentname.Text);
            }

            if (department["boid"].ToString() != ddlBo.SelectedValue)
            {
                content = string.Format("修改了部门信息。分公司：{0}->{1}", department["boid"], ddlBo.SelectedValue);
            }

            AddLog(content);

            department["departmentname"] = txb_departmentname.Text;
            department["boid"] = ddlBo.SelectedValue;

            PhoneBook.Group.Department.edit(department);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }   
    }
}